package bTree.LeetCode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class 两个数组的交集II {
    public int[] intersect(int[] nums1, int[] nums2) {
        int[] target = new int[2000];
        int cnt = 0;
        HashMap<Integer,Integer>map1=new HashMap<>();
        for (int i : nums1) {
            if(!map1.containsKey(i)){
                map1.put(i,1);
            }else {
                map1.put(i,map1.get(i)+1);
            }
        }
        for (int i : nums2) {
           if(map1.containsKey(i)&&map1.get(i)>0){
               target[cnt++]=i;
               map1.put(i,map1.get(i)-1);
           }
        }



        return Arrays.copyOfRange(target, 0, cnt);
    }
}